package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.LetterChatBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/29.
 */
public class LetterChatAdapter extends BaseAdapter {

    private ArrayList<LetterChatBean> letterChatList;

    private Context context;

    public LetterChatAdapter(Context context,ArrayList<LetterChatBean> letterChatList){
        this.context = context;

        this.letterChatList = letterChatList;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return letterChatList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_of_letter_chat,null);
            holder = new ViewHolder();
            holder.timeText = (TextView) view.findViewById(R.id.item_of_letter_chat_time_text);
            holder.otherLayout = (LinearLayout) view.findViewById(R.id.item_of_letter_chat_other_layout);
            holder.otherHeaderImageView = (ImageView) view.findViewById(R.id.item_of_letter_chat_other_header_image_view);
            holder.otherContentText = (TextView) view.findViewById(R.id.item_of_letter_chat_other_content_text);

            holder.selfLayout = (LinearLayout) view.findViewById(R.id.item_of_letter_chat_self_layout);
            holder.selfHeaderImageView = (ImageView) view.findViewById(R.id.item_of_letter_chat_self_header_image_view);
            holder.selfContentText = (TextView) view.findViewById(R.id.item_of_letter_chat_self_content_text);

            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
            resetViewHolder(holder);
        }

        LetterChatBean bean = letterChatList.get(position);

        holder.timeText.setText(bean.getTimeStr());

        String type = bean.getTypeStr();
        if (type.equals("self")){
            holder.selfLayout.setVisibility(View.VISIBLE);
            holder.otherLayout.setVisibility(View.GONE);
            holder.selfContentText.setText(bean.getContentStr());
            holder.selfHeaderImageView.setImageResource(R.drawable.ic_launcher);
        }else {
            holder.otherLayout.setVisibility(View.VISIBLE);
            holder.selfLayout.setVisibility(View.GONE);
            holder.otherContentText.setText(bean.getContentStr());
            holder.otherHeaderImageView.setImageResource(R.drawable.ic_launcher);
        }

        return view;
    }

    private void resetViewHolder(ViewHolder holder){
        holder.timeText.setText(null);

        holder.otherContentText.setText(null);

        holder.otherHeaderImageView.setImageBitmap(null);

        holder.selfContentText.setText(null);

        holder.selfHeaderImageView.setImageBitmap(null);
    }

    private class ViewHolder{
        private TextView timeText;

        private ImageView otherHeaderImageView;

        private TextView otherContentText;

        private LinearLayout otherLayout;

        private LinearLayout selfLayout;

        private ImageView selfHeaderImageView;

        private TextView selfContentText;
    }
}
